<?php
declare (strict_types = 1);

namespace app\service\logic\common\scale;

use app\exception\LogicException;
use app\facade\ALiPayFacade;
use app\model\communal\OrderMultiModel;
use app\model\communal\ScaleMultiModel;
use app\service\logic\BaseService;
use app\service\logic\common\order\OrderMultiService;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Model;

final class MultiService extends BaseService
{
    protected string $modelClassName = 'ScaleMultiModel';

    /**
     * 排序参数
     *
     * @param $params
     * @param string $column
     * @param string $type
     * @return array|string[]
     */
    public function orderByParams($params, string $column = 'id', string $type = 'desc')
    {
        if (
            isset($params['order_option']) &&
            in_array($params['order_option'],[ScaleMultiModel::ORDER_OPTION_ONE, ScaleMultiModel::ORDER_OPTION_TWO, ScaleMultiModel::ORDER_OPTION_THREE])
        ) {
            $orderField = ScaleMultiModel::$orderRuleMap[$params['order_option']]['orderBy'];
            $orderDir = ScaleMultiModel::$orderRuleMap[$params['order_option']]['orderDir'];
            $order = [$orderField => $orderDir, 'id' => 'desc'];
            unset($params['order_option']);
        } else {
            $orderField =  isset($params['orderBy']) && !empty($params['orderBy']) ? $params['orderBy'] : $column;
            $orderDir =  isset($params['orderDir']) && !empty($params['orderDir']) ? $params['orderDir'] : $type;
            $order = [$orderField => $orderDir];
        }

        return $order;
    }

    /**
     * 销量 + 1
     *
     * @param $id
     * @return array|mixed|Model
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function salesNumberPlus($id)
    {
        $model = $this->getSimpleDetail(['id' => $id]);
        $model->save(['sales_number' => $model['sales_number'] + 1]);
        return $model;
    }
}
